// pages/bookEnter/ranking/index.js
const appData = getApp()
Component({
  lifetimes: {
    attached() {
      this.getGenderData() // 获取gender初始数据
    },
    // 在组件实例进入页面节点树时执行
    detached() {
      // 在组件实例被从页面节点树移除时执行
    }
  },
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    gender: {},
    arrLength: 0,
    bgColor: ["#eee", "", "", "", ],

    _id: "",
    data: "",
    allDataBooks: "",
    dataBooks: "",
    erIndex: [], // 二级数据的列表背景颜色改变

    opacity: 0,
    flog: 1,
    ADDNUM: 20,

    hasBang: 0,
    hasErBang: 0,

    idArr: {}, // 获取榜单按钮的id数据用来切换数据
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getGenderData() {
      let ranking = appData.globalData.ranking;
      if (ranking.length > 0) {
        this.setData({
          gender: ranking
        })

        this.saveAppData()
      } else {
        setTimeout(() => {
          this.getGenderData()
        }, 300)
      }
    },

    saveAppData() {
      let index = appData.globalData.index;
      this.saveFirdata(index) // 保存上一次一级目录表的数据位置信息
      let erIndex = appData.globalData.erIndex;
      let hasBang = appData.globalData.hasBang;
      this.setData({
        arrLength: index
      })

      let length = this.data.gender[index].data.length;
      let arr = [];
      arr.length = length;
      arr[erIndex] = "#fff";
      this.setData({
        erIndex: arr
      })

      this.setData({
        hasBang
      })
      this.getData(index, erIndex, 0) // 获取数组的0下标的第一个值
    },

    saveFirdata(data) { // 保存上一次一级目录表的数据位置信息
      let bgColor = this.data.bgColor;
      bgColor = bgColor.map((res, index) => {
        if (index === data) {
          res = "#eee"
        } else {
          res = ""
        }
        return res;
      })
      this.setData({
        bgColor
      })
    },

    toggleArrLength(e) {
      this.toggleErDataIndex(0);

      let num = parseInt(e.target.id);
      this.setData({
        arrLength: num
      })

      this.kongzhiBang() // 控制榜的按钮是否显示

      this.saveFirdata(num) // 保存上一次一级目录表的数据位置信息
      this.getData(this.data.arrLength, 0, 1)
    },

    getSecondData(e) {
      let er = e.currentTarget.id;
      this.toggleErDataIndex(er)

      if (this.data.gender[this.data.arrLength].data[er].monthRank) {
        this.setData({
          hasBang: 1
        })
        appData.sethasBang(1)
      } else {
        this.setData({
          hasBang: 0
        })
      }

      this.getData(this.data.arrLength, er, 1);
    },

    toggleErDataIndex(num) {
      let erIndex = this.data.erIndex;
      let length = erIndex.length;
      let arr = [];
      arr.length = length;
      arr[num] = "#fff";
      this.setData({
        erIndex: arr
      })
    },


    getData(index, erIndex, bool, newId) {
      this.setData({
        dataBooks: ""
      })
      appData.setIndex(index);
      appData.setErIndex(erIndex);

      let id = newId || this.data.gender[index].data[erIndex]._id;

      if (appData.globalData.bgData.books && !bool) {
        let ranking = appData.globalData.bgData;
        this.setData({
          data: ranking
        })
        this.setData({
          allDataBooks: ranking.books
        })
        this.setData({
          dataBooks: ranking.books.slice(0, 40)
        })
        // this.kongzhiBang() // 控制榜的按钮是否显示
      } else {
        let that = this;
        wx.request({
          url: `https://api.zhuishushenqi.com/ranking/${id}`,
          success: res => {
            let data = res.data.ranking;
            data.books.forEach(val => {
              let src = val.cover.indexOf("http");
              src = decodeURIComponent(val.cover.substring(src)); // 解码状态
              val.cover = src;
            }) // 解码图片的地址并且获取到正确数据

            // if (data.gender === "male") {
            //   data.gender = "男生专区"
            // } else if (data.gender === "female") {
            //   data.gender = "女生专区"
            // } else if (data.gender === "picture") {
            //   data.gender = "出版专区"
            // } else if (data.gender === "epub") {
            //   data.gender = "电子书专区"
            // } // 更改来源英文显示切换中文显示        

            let num = data.updated.indexOf("T");
            data.updated = data.updated.slice(0, num);
            // 更改时间显示

            let ranking = res.data.ranking;
            that.setData({
              data: ranking
            })
            that.setData({
              allDataBooks: ranking.books
            })
            that.setData({
              dataBooks: ranking.books.slice(0, 40)
            })


            that.kongzhiBang(newId) // 控制榜的按钮是否显示
            appData.setBgData(that.data.data)
            // that.setData({
            //   dataBooks: res.data.ranking.books.slice(0, that.data.ADDNUM)
            // }) // 做滚动条下拉加载。。。
          }
        })


      }
    },

    kongzhiBang(newId) { // 控制榜的按钮是否显示
      if (this.data.data.monthRank) {
        this.setData({
          idArr: {
            _id: this.data.data._id,
            monthRank: this.data.data.monthRank,
            totalRank: this.data.data.totalRank
          }
        })
      }

      if (this.data.data.monthRank || newId) {
        appData.sethasBang(1)
        this.setData({
          hasBang: 1
        })
      } else {
        appData.sethasBang(0)
        this.setData({
          hasBang: 0
        })
      }
    },

    toggleBangData(e) {
      let newId = e.currentTarget.id;
      let index = appData.globalData.index;
      let erIndex = appData.globalData.erIndex;
      this.getData(index, erIndex, 1, newId)
    },

    toggleBangShow() {
      let bool = this.data.hasErBang;
      bool ? bool = 0 : bool = 1
      this.setData({
        hasErBang: bool
      })
    },


    hiddenBang() {
      this.setData({
        hasErBang: 0
      })
    },
  }
})